package com.module.product.dao;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;

import base.dao.GenericHibernateDao;
import base.util.Page;

import com.module.product.common.ProductConstant;
import com.module.product.vo.ProductTemporary;

/**
*
* 商品dao类
*
* @author  王勇云
* @version  [V1.0, Jul 22, 2011]
* @see  [相关类/方法]
* @since  [二代服务平台/商品]
*/
public class ProductTemporaryDao extends GenericHibernateDao<ProductTemporary,String>{
	private static final ProductTemporaryDao instance = new ProductTemporaryDao();
	
	private ProductTemporaryDao(){}
	
	public synchronized static ProductTemporaryDao getInstance(){
		return instance;
	}
	
	public Page getList(Session session,ProductTemporary obj,Page page){
        String hql1 = "from ProductTemporary as obj where obj.approveState = "+ProductConstant.APPROVE_WAIT_APPROVE;
        String hql2 = "select count(*) from ProductTemporary as obj  where obj.approveState ="+ProductConstant.APPROVE_WAIT_APPROVE;
        if(StringUtils.isNotBlank(obj.getName())){
            hql1=hql1 +  " and obj.name like '%"+obj.getName()+"%'";
            hql2=hql2 +  " and obj.name like '%"+obj.getName()+"%'";
        }
        if(StringUtils.isNotBlank(obj.getOrgName())){
            hql1=hql1 +  " and obj.orgName like '%"+obj.getOrgName()+"%'";
            hql2=hql2 +  " and obj.orgName like '%"+obj.getOrgName()+"%'";
        }
        if(StringUtils.isNotBlank(obj.getDesc())){
            hql1=hql1 +  " and obj.desc like '%"+obj.getDesc()+"%'";
            hql2=hql2 +  " and obj.desc like '%"+obj.getDesc()+"%'";
        }
        String hql3=" ORDER BY obj.updateTime DESC,obj.createTime DESC ";
        
        Query query = session.createQuery(hql1+hql3);
              query.setFirstResult(page.getStart()).setMaxResults(page.getLimit());
        
        page.setTotal((long) ((Long) session.createQuery(hql2)
            .uniqueResult()).intValue());
        page.setRows(query.list());
        return page;
	}
	public List<ProductTemporary> getHistoryByEntProductId(Session session,String entProductId){
		return session.createQuery("from ProductTemporary where entProductId in("+entProductId+")").list();
	}
	public ProductTemporary getProductTemporary(Session session,String entProductId) {
		List<ProductTemporary> list = session.createQuery("from ProductTemporary where entProductId ='"+entProductId+"' and approveState ="+ProductConstant.APPROVE_WAIT_APPROVE+" order by createTime desc ").list();
		if(!list.isEmpty() && list.size()>0){
			return list.get(0);
		}
		 throw null;
		
	}
}
